<!DOCTYPE html>
<html lang="zh-cn">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>JS007-生成柱形图案例</title>
</head>
<style>
  body {
    background-color: #222;
  }

  .bar-chart {
    display: flex;
    justify-content: space-evenly;
    align-items: flex-end;
    height: 300px;
    border: 2px solid pink;
  }

  .bar {
    position: relative;
    width: 50px;
    /* margin: 0 5px; */
    background-color: pink;
  }

  .bar span {
    position: absolute;
    bottom: 100%;
    /* transform: translateY(10%); */
    width: 100%;
    text-align: center;
    color: snow;
    font-weight: bold;
  }

  .bar h4 {
    position: absolute;
    top: 100%;
    transform: translateY(-80%);
    width: 100%;
    text-align: center;
    color: snow;
  }
</style>

<body>
  <script>
    let arr = [5, 10, 15, 20, 25, 22, 40, 83, 2]
    // let arr = []
    // let cnt = +prompt('输入柱子个数')
    // for (let i = 0; i < cnt; i++) {
    //   arr.push(+prompt(`输入第${i + 1}个数据`))
    // }
    document.writeln('<div class="bar-chart">')
    for (let i = 0; i < arr.length; i++) {
      document.write(`
        <div class="bar" style="height: ${arr[i] * 2}px;">
          <span>${arr[i]}</span>
          <h4>第${i + 1}项</h4>
        </div>
      `)
    }
    document.writeln('</div>')
  </script>
</body>

</html>